using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class JobSeekers_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Contents["jsuserid"] != null)
        {
            Response.Redirect("myaccount.aspx");
        }
        if (Request.Cookies["Remember"] != null)
        {
            HttpCookie httpCookie =
            Request.Cookies["EmailAndTime"];
            txtEmail.Text = httpCookie.Values["Email"].ToString();

        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string[] valueToGet = new string[3] { "", "", "" };
        SQLServerProviders cls = new SQLServerProviders();
        
        valueToGet=cls.LoginAuthentication(txtEmail.Text,txtPassword.Text);
        if (valueToGet[0]== "OK")
        {
            Session.Add("jsuserid", valueToGet[1]);
            Session.Add("jsemail", txtEmail.Text);
            Session.Add("jsfullname", valueToGet[2]);
            WriteCookie(txtEmail.Text);
            Response.Redirect("myaccount.aspx");
        }
        else
        {
            switch(valueToGet[0])
            {
                case "U":
                    lblHeader.Text="Email is not existing,"+
                    " please re-enter your email and try again.";
                    break;
                case "P":
                    lblHeader.Text = "Invalid password, "+
                    "please re-enter your own password and try again.";
                    break;
                default:
                    lblHeader.Text = "System error, " + cls.ErrorMessage;
                    break;
            }
        }
    }
    void WriteCookie(string email)
    {
        if (CheckBoxRememberEmail.Checked)
        {
            HttpCookie httpCookie = new HttpCookie("Remember", "Yes");
            Response.Cookies.Add(httpCookie);
            httpCookie = new HttpCookie("EmailAndTime");
            httpCookie.Values.Add("Email",email);
            httpCookie.Values.Add("Time", DateTime.Now.ToShortDateString());
            Response.Cookies.Add(httpCookie);
        }
    }
}
